340C - Tourist Problem - CodeForces Solution


combinatorics implementation math *1600

Please click on ads to support us..

C++ Code:

// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC optimize("O3")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

#define int long long

#define                                   fi first
#define                                   se second
#define                                 pb emplace_back
#define                                 pf emplace_front
#define                            SZ(x) ((int)(x).size())
#define                           ALL(x) (x).begin(),(x).end()
#define                          RALL(x) (x).rbegin(),(x).rend()
#define     F_OR(i, a, b, s) for (int i = (a); (s) > 0 ? i <= (b) : i >= (b); i += (s))
#define                             F_OR1(e) F_OR(i, 0, (e), 1)
#define                          F_OR2(i, e) F_OR(i, 0, (e) - 1, 1)
#define                           F_OR3(i, b, e) F_OR(i, b, (e), 1)
#define                          F_OR4(i, b, e, s) F_OR(i, b, (e), s)
#define                             GET5(a, b, c, d, e, ...) e
#define                 F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define                      FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define file(task) if(fopen(task".INP", "r")){freopen(task".INP", "r", stdin); freopen(task".OUT", "w", stdout);}

/****************[BIT]****************/
#define     getbit(x, i) ((x >> (i)) & 1)
#define     lowbit(x) __builtin_ctzll(x)    
#define     topbit(x) 63 - __builtin_clzll(x)
#define     cntbit(x) __builtin_popcountll(x)                   
/****************[END]****************/

using ll = long long;
using ld = double;
using ii = pair <int, int>;
using _i3 = pair <ii, int>;
using _i4 = pair <ii, ii>;
using vi2 = vector <ii>;

template<class A, class B> bool maxi(A& a, const B& b) { return (a < b ? a = b, 1 : 0); }
template<class A, class B> bool mini(A& a, const B& b) { return (a > b ? a = b, 1 : 0); }
template<class T> using MaxHeap = priority_queue< T, vector<T>, less<T> >;
template<class T> using MinHeap = priority_queue< T, vector<T>, greater<T> >;

const int INF = (int)1e9 + 5062007;
const ll LINF = (ll)8e18 + 5062007;
const long double EPS = (double)1e-14;
const long double PI = acos(-1.0);
//const int MOD = 998244353;
//const int MOD = (int)1e9 + 7;
const int MOD = 123456789;
void solve(int test_case);

signed main() {
    cin.tie(nullptr), cout.tie(nullptr)->sync_with_stdio(0);
    file("phuc");
    cout.precision(15);
    cout << fixed;
    int _ = 1;
    //cin >> _;
    FOR(__, 1, _) solve(__);
    cerr << "Time elapsed: " << 1.0 * clock() / (double)CLOCKS_PER_SEC << " s.\n";
    return 0;
}

/*

*/
/*
SOLUTION ========================/
--------------------------------+/

--------------------------------+/
=================================/
*/
const int MAX = 1e5 + 5;
const int LOG = 18;

int inv[MAX], ifact[MAX], fact[MAX];
struct combine {
    combine() {
        inv[1] = 1;
        FOR(i, 2, MAX - 5) {
            inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
        }
        fact[0] = ifact[0] = 1;
        FOR(i, 1, MAX - 5) {
            fact[i] = fact[i - 1] * i % MOD;
            ifact[i] = ifact[i - 1] * inv[i] % MOD;
        } 
    }
} combine_;

int comb(int n, int m) {
    if(n < m || m < 0) return 0;
    return fact[n] * ifact[m] % MOD * ifact[n - m] % MOD;
}
int n;
int a[MAX];
void solve(int test_case) {
    // cout << "Case #" << test_case << ": ";
    cin >> n;
    int sum = 0;
    FOR(i, 1, n) cin >> a[i], sum += a[i];
    int sum_prev = 0, now = 0;
    sort(a + 1, a + 1 + n);
    FOR(i, 1, n) {
        now += a[i] * (i - 1) - sum_prev;
        sum_prev += a[i];
    }
    sum = now * 2 + sum;
    int d = gcd(sum, n);
    cout << sum / d << ' ' << n / d;
}


Comments

Submit
0 Comments
More Questions

1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String